package org.balthie.demo.mat.histogram;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.balthie.demo.opensource.apache.poi.SSXS.XSSFUtils;

public class GroupMapBuider
{
    static Map<String, PackageMATInfo> packageMap = new HashMap<>(100);
    
    static List<String> sortedMapKey = new ArrayList<>(100);
    
    static String templetePath = "/excel/SXSSF/按包统计类数量.xlsx";
    
    public static void buildGroupMap(String targetPath)
    {
        String path = templetePath;
        Function<XSSFCell, PackageMATInfo> mapper = new TempleteMapper();
        if(!StringUtils.isEmpty(targetPath))
        {
            path = targetPath;
            mapper = new DynamicMapper();
        }
        builderFromTemplete(path, mapper);
    }
    
    private static void builderFromTemplete(String path, Function<XSSFCell, PackageMATInfo> mapper)
    {
        XSSFWorkbook templeteSWB = null;
        try
        {
            templeteSWB = XSSFUtils.readExcel(path);
            XSSFSheet templeteSheet = templeteSWB.getSheetAt(0);
            XSSFRow row = null;
            XSSFCell cell = null;
            PackageMATInfo p = null;
            for(int i = 1; i <= templeteSheet.getLastRowNum(); i++)
            {
                row = templeteSheet.getRow(i);
                if(row != null)
                {
                    cell = row.getCell(0);
                    if(cell != null)
                    {
                        p = mapper.apply(cell);
                        if(p != null)
                        {
                            if(!packageMap.containsKey(p.getName()))
                            {
                                sortedMapKey.add(p.getName());
                                packageMap.putIfAbsent(p.getName(), p);
                            }
                        }
                    }
                }
            }
            System.out.println(sortedMapKey.size());
        }
        finally
        {
            try
            {
                if(templeteSWB != null)
                {
                    templeteSWB.close();
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    
    static class TempleteMapper implements Function<XSSFCell, PackageMATInfo>
    {
        @Override
        public PackageMATInfo apply(XSSFCell t)
        {
            return new PackageMATInfo(t.getStringCellValue());
        }
    }
    
    static class DynamicMapper implements Function<XSSFCell, PackageMATInfo>
    {
        @Override
        public PackageMATInfo apply(XSSFCell t)
        {
            String origPackage = t.getStringCellValue();
            String analyPackage = parse(origPackage);
            PackageMATInfo p = packageMap.get(analyPackage);
            if(p == null)
            {
                System.out.println("GroupMapBuider add analyPackage : " + analyPackage + " , orig packate " + origPackage);
                return new PackageMATInfo(analyPackage);
            }
            else
            {
                return null;
            }
        }
        
        private String parse(String origPackage)
        {
            if(origPackage.startsWith("Fastjson_ASM"))
            {
                return "Fastjson_ASM";
            }
            else if(origPackage.startsWith("libthrift091"))
            {
                return "libthrift091";
            }
            else if(origPackage.startsWith("rx"))
            {
                return "rx";
            }
            else if(origPackage.startsWith("feign"))
            {
                return "feign";
            }
            else if(origPackage.startsWith("Serializer_"))
            {
                return "Serializer_";
            }
            else if(origPackage.startsWith("antlr"))
            {
                return "antlr";
            }
            else if(origPackage.startsWith("okhttp3"))
            {
                return "okhttp3";
            }
            else if(origPackage.startsWith("javassist"))
            {
                return "javassist";
            }
         /*   else if(origPackage.startsWith("sun"))
            {
                return "sun";
            }*/
            else if(origPackage.startsWith("okio"))
            {
                return "okio";
            }
            else if(origPackage.startsWith("javax"))
            {
                return "javax";
            }
            else if(origPackage.startsWith("java"))
            {
                return "java";
            }
            
            
            String[] divideName = origPackage.split("\\.");
            if(divideName.length >= 3)
            {
                if(origPackage.startsWith("com.viomi.component"))
                {
                    divideName = Arrays.copyOf(divideName, 4);
                }
                else if(origPackage.startsWith("com.viomi"))
                {
                    divideName = Arrays.copyOf(divideName, 3);
                }
                else  if(origPackage.startsWith("sun"))
                {
                    divideName = Arrays.copyOf(divideName, 2);
                }
                else if(origPackage.startsWith("org.apache"))
                {
                    divideName = Arrays.copyOf(divideName, 4);
                }
                else 
                {
                    divideName = Arrays.copyOf(divideName, 2);
                }
            }
            
            return StringUtils.join(divideName, ".");
        }
    }
    
}
